package com.lr.chat;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


public class Server {
	
	private Map<String,MyChannel> lists = new HashMap<String,MyChannel>();
	
	public static void main(String[] args) throws IOException {
		new Server().start();
	}
	
	public void start(){
		//1.创建服务端+端口
		ServerSocket server = null;
		try {
			server = new ServerSocket(8888);
		} catch (IOException e) {
			e.printStackTrace();
		}  
		Socket socket = null;
		while(true){
		    try {
				socket = server.accept();
			} catch (IOException e) {
				e.printStackTrace();
			}
		    MyChannel myChannel = new MyChannel(socket);
		    Thread myThread = new Thread(myChannel);
		    myThread.start();
		    lists.put(myChannel.name, myChannel);
		}
	}
	
	class MyChannel implements Runnable{
		
		private DataInputStream dis;
		
		private DataOutputStream dos;
		
		private boolean isRunning = true;
		
		public String name;
		
		public MyChannel(Socket socket){
			try {
				dos = new DataOutputStream(socket.getOutputStream());
				dis = new DataInputStream(socket.getInputStream());
				name = recieve();
				send("欢迎您进入聊天室");
				sendAll("欢迎"+name+"进入聊天室",true);
			} catch (IOException e) {
				send("=========您已经退出聊天室===========");
				sendAll(name+"退出聊天室",true);
				stop();
				IOUtil.IOClose(dos,dis);
				lists.remove(this);
			}
		}
		
		public void stop(){
			isRunning = false;
		}
		
		public String recieve(){
			String msg = null;
			try {
				msg = dis.readUTF();
				System.out.println(msg);
			} catch (IOException e) {
				send("=========您已经退出聊天室===========");
				sendAll(name+"退出聊天室",true);
				stop();
				IOUtil.IOClose(dos,dis);
				lists.remove(this);
			}
			return msg;
		}
		
		public boolean send(String msg){
			if(null == msg || ("").equals(msg)){
				return false;
			}
			try {
				dos.writeUTF(msg);
				dos.flush();
			} catch (IOException e) {
				send("=========您已经退出聊天室===========");
				sendAll(name+"退出聊天室",true);
				stop();
				IOUtil.IOClose(dos,dis);
				lists.remove(this);
				return false;
			}
			return true;
		}
		
		public void sendAll(String msg,boolean isSystem){
			
			if(msg.startsWith("@") && msg.indexOf(":") > -1){
				String obName = msg.substring(1, msg.indexOf(":"));
				String data = msg.substring(msg.indexOf(":")+1);
				MyChannel temp = lists.get(obName);
				temp.send(this.name+"给您发送了悄悄话："+data);
			}else if("exit".equals(msg)){
				send("=========您已经退出聊天室===========");
				sendAll(name+"退出聊天室",true);
				stop();
				IOUtil.IOClose(dos,dis);
				lists.remove(this);
			}
			else{
			
			    if(isSystem){
				    msg = "系统信息：" + msg;
			    }
			    else{
				    msg = this.name+":"+msg;
			    }
			    for(String value : lists.keySet()){
				    MyChannel temp = lists.get(value);
				    if(temp == this){
					    continue;
				     }
				     temp.send(msg);
			    }
			}

		}

		@Override
		public void run() {
			while(isRunning){
				//boolean flag = send(recieve());
				String msg = recieve();
				sendAll(msg,false);
			}
		}
	}

}

